package competition.controller;

import competition.po.TVersionEntity;
import competition.service.VersionService;
import competition.vo.AreaName;
import competition.vo.Upload;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/10/30/030.
 */
@Controller
@RequestMapping(value = "version")
public class VersionController {

    @Resource(name = "versionService")
    private VersionService versionService;

    /**
     * 版本上传
     * @param name
     * @param apk
     * @param request
     * @return
     */
    @RequestMapping(value = "insertVersion",method = RequestMethod.POST)
    @ResponseBody
    public Map insertVersion(@RequestParam String name, @RequestParam MultipartFile apk, HttpServletRequest request){

        String apk_url= Upload.upload(apk,request);
        Map map=new HashMap();
        if (versionService.insertVersion(name,apk_url)){
            map.put("message","版本上传成功");
            map.put("status",200);
        }else{
            map.put("message","版本上传失败");
            map.put("status",201);
        }
        return map;
    }

    /**
     * 获取版本信息
     * @return
     */
    @RequestMapping(value = "returnVersion",method = RequestMethod.POST)
    @ResponseBody
    public Map returnVersion(HttpServletRequest request){
        List<TVersionEntity> list=versionService.returnVersion();

        for (int i=0;i<list.size();i++){
            list.get(i).setApk(request.getSession().getServletContext().getRealPath("/")+list.get(i).getApk());
        }
        Map map=new HashMap();
        map.put("result",list);
        map.put("length",list.size());
        return map;
    }

    @RequestMapping(value = "downloadVersion",method = RequestMethod.GET)
    @ResponseBody
    public Map downloadVersion(HttpServletRequest request, HttpServletResponse response){
        List<TVersionEntity> list=versionService.returnVersion();
        String apk_temp=list.get(0).getApk();
        String apk_name=list.get(0).getName();
        response.setContentType("text/html;charset=utf-8");
        try {
            request.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        java.io.BufferedInputStream bis = null;
        java.io.BufferedOutputStream bos = null;

        String ctxPath = request.getSession().getServletContext().getRealPath("/");
        String downLoadPath = ctxPath + apk_temp;
        System.out.println(downLoadPath);
        try {
            long fileLength = new File(downLoadPath).length();
            response.setContentType("application/x-msdownload");
            response.setHeader("Content-Length", String.valueOf(fileLength));
            response.setHeader("Content-disposition", "attachment; filename=" + new String(apk_name.getBytes("utf-8"), "ISO8859-1"));
            bis = new BufferedInputStream(new FileInputStream(downLoadPath));
            bos = new BufferedOutputStream(response.getOutputStream());
            byte[] buff = new byte[2048];
            int bytesRead;
            while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                bos.write(buff, 0, bytesRead);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bis != null)
                try {
                    bis.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            if (bos != null)
                try {
                    bos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }


        return null;
    }





}
